package com.endurance.controller;

import com.endurance.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class Test05RestfulController {

    @GetMapping("test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(request,response);
    }

    @GetMapping("/{username}")
    public @ResponseBody User findByName(@PathVariable("username") String username) {
        return new User(username);
    }

    @PostMapping
    public String saveUser(@RequestBody User user) {
        // 保存用户信息
        System.out.println(user);
        return "success";
    }

    @PutMapping//.jsp页面不支持PUT// 请求
    public String updateUser( User user) {
        // 更新用户信息
        System.out.println(user);
        return "success";
    }

    @DeleteMapping("{id}")
    public String deleteUser(@PathVariable("id") Integer uid) {
        // 删除用户信息
        System.out.println(uid);
        return "success";
    }

}
